package cn.hqm.hqmtechnology.service.impl;

import cn.hqm.hqmtechnology.dao.dto.MemberInfoDTO;
import cn.hqm.hqmtechnology.dao.mapper.MemberInfoMapper;
import cn.hqm.hqmtechnology.dao.po.MemberInfo;
import cn.hqm.hqmtechnology.dao.po.MemberRole;
import cn.hqm.hqmtechnology.service.MemberInfoService;
import cn.hqm.hqmtechnology.service.MemberRoleService;
import cn.hqm.hqmtechnology.utils.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Member;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class MemberInfoServiceImpl extends ServiceImpl<MemberInfoMapper, MemberInfo> implements MemberInfoService {

    @Resource
    private MemberRoleService memberRoleService;
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private MemberInfoService memberInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addMemberInfo(MemberInfoDTO memberInfoDTO) {
        // 注册用户
        boolean flag = true;
        if (Objects.isNull(memberInfoDTO.getName())) {
            flag = false;
        }
        if (Objects.isNull(memberInfoDTO.getAccount()) || memberInfoDTO.getAccount().length() < 6) {
            flag = false;
        }
        if (Objects.isNull(memberInfoDTO.getPassword())) {
            flag = false;
        }
        String regex = "^[a-zA-Z0-9_+.-]+@[a-zA-Z0-9.-]+$";
        if (Objects.isNull(memberInfoDTO.getEmail()) || !memberInfoDTO.getEmail().matches(regex)) {
            flag = false;
        }
        regex = "^1[3456789]\\d{9}$";
        // 手机号规则校验
        if (Objects.isNull(memberInfoDTO.getPhone()) || !memberInfoDTO.getPhone().matches(regex)) {
            flag = false;
        }
        if (!flag) {
            throw new RuntimeException("传参异常!");
        }
        // 账号不能重复
        long count = this.count(new LambdaQueryWrapper<MemberInfo>().eq(MemberInfo::getAccount, memberInfoDTO.getAccount()).or().eq(MemberInfo::getPhone, memberInfoDTO.getPhone()));
        if (count > 0) {
            throw new RuntimeException("该账号或手机号码已注册,请更换!");
        }


        // 创建member对象
        MemberInfo memberInfo = new MemberInfo();
        Long id = IdGen.id();
        BeanUtils.copyProperties(memberInfoDTO, memberInfo);
        memberInfo.setId(id);
        // 创建对应角色
        MemberRole memberRole = new MemberRole();
        memberRole.setMemberId(id);
        memberRole.setId(IdGen.id());
        // 普通用户
        memberRole.setType(1);
        this.save(memberInfo);
        memberRoleService.save(memberRole);
        return true;
    }

    @Override
    public Object login(MemberInfoDTO memberInfoDTO) {
        // 用户登录并返回token
        String account = memberInfoDTO.getAccount();
        String password = memberInfoDTO.getPassword();
        Integer checkCode = memberInfoDTO.getCheckCode();

        // 校验参数
        if (Objects.isNull(account)) {
            throw new RuntimeException("账号参数异常!");
        }
        // 校验参数
        if (Objects.isNull(password)) {
            throw new RuntimeException("密码参数异常!");
        }
        // 校验参数
        if (Objects.isNull(checkCode)) {
            throw new RuntimeException("验证码参数异常!");
        }
        // 校验验证码
        Object forValue = redisUtil.getForValue(checkCode.toString());
        if (Objects.isNull(forValue)) {
            throw new RuntimeException("验证码失效,请重新获取!");
        }
        // 校验账号密码
        MemberInfo memberInfo = memberInfoService.getOne(new LambdaQueryWrapper<MemberInfo>().eq(MemberInfo::getAccount, account).eq(MemberInfo::getPassword, password).eq(MemberInfo::getDeleted,0));
        if (Objects.isNull(memberInfo)) {
            throw new RuntimeException("账号密码错误,请检查!");
        }

        // 生成token
        HashMap<String, Object> data = new HashMap<>();
        data.put("memberId",memberInfo.getId().toString());
        data.put("phone",memberInfo.getPhone());
        data.put("email",memberInfo.getEmail());
        data.put("name",memberInfo.getName());
        data.put("account",memberInfo.getAccount());
        String token = JWTUtil.generateToken(data);
        redisUtil.setForValueOverTime(token,data,24L, TimeUnit.HOURS);
        return token;
    }

    @Override
    public Object info(MemberInfoDTO memberInfoDTO) {
        if (Objects.isNull(memberInfoDTO) || ObjectUtils.allPropertiesEmpty(memberInfoDTO)) {
            return ThreadLocalUtil.threadLocalMap.get();
        }
        return null;
    }

    @Override
    public Object logout(MemberInfoDTO memberInfoDTO) {
        return true;
    }
}